package com.cindata

import com.cindata.dictionary.*
import grails.converters.JSON
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Transactional
@Secured(['isAuthenticated()'])
class AccountInspectController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def springSecurityService

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AccountInspect.list(params), model:[accountInspectCount: AccountInspect.count()]
    }

    def show(AccountInspect accountInspect) {
        respond accountInspect
    }

    def create() {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        println params?.account
        def user = User.findById(springSecurityService.principal.id)
        respond new AccountInspect(params),model:[account:Account.findById(Long.parseLong(params?.id)),user:user]
    }

    @Transactional
    def save(AccountInspect accountInspect) {
        def user = User.findById(springSecurityService.principal.id)
        accountInspect.status = "Pending"
        accountInspect.startTime = new Date()
        accountInspect.createBy = user
        accountInspect.user = user
        accountInspect.save flush:true
        println accountInspect?.errors
        redirect url: params['targetUri']
    }

    def edit(AccountInspect accountInspect) {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        println params?.account
        def user = User.findById(springSecurityService.principal.id)
        respond accountInspect,model:[account:Account.findById(accountInspect?.account?.id),user:user]
    }

    def editSave(AccountInspect accountInspect){
        accountInspect.save flush:true
        println accountInspect?.errors
        redirect url: params['targetUri']
    }

    @Transactional
    def update(AccountInspect accountInspect) {
        if (accountInspect == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountInspect.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond accountInspect.errors, view:'edit'
            return
        }

        accountInspect.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'accountInspect.label', default: 'AccountInspect'), accountInspect.id])
                redirect accountInspect
            }
            '*'{ respond accountInspect, [status: OK] }
        }
    }

    @Transactional
    def delete(AccountInspect accountInspect) {

        if (accountInspect == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountInspect.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountInspect.label', default: 'AccountInspect'), accountInspect.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountInspect.label', default: 'AccountInspect'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
